﻿// Copyright (c) 2007-2021 西安交通信息投资营运有限公司 版权所有
// 作者:吕艳阳

using DevExpress.Utils.Design;
using DevExpress.XtraCharts;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using XCI.Core;
using XCI.Helper;
using XCI.Setting;

namespace XCI.Windows
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class ChartControlPlus : ChartControl, ISettingSerializer
    {
        /// <summary>
        /// 配置标识
        /// </summary>
        [Category("XCI"), Description("配置标识")]
        [SmartTagProperty("配置标识", "XCI", SmartTagActionType.RefreshAfterExecute)]
        public virtual string Id { get; set; } = $"chart-{StringHelper.Guid()}";

        /// <summary>
        /// 允许自定义配置
        /// </summary>
        [Category("XCI"), Description("允许自定义配置"), DefaultValue(true)]
        [SmartTagProperty("允许自定义配置", "XCI", SmartTagActionType.RefreshAfterExecute)]
        public bool AllowCustomSetting { get; set; } = true;

        [Category("XCI"), Description("自动加载配置"), DefaultValue(true)]
        [SmartTagProperty("自动加载配置", "XCI", SmartTagActionType.RefreshAfterExecute)]
        public bool AutoLoadSetting { get; set; } = true;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public SettingStoreManager SettingManager { get; set; }

        [Category("XCI"), Description("加载配置之前事件")]
        public event EventHandler<EventArgs> BeforeRestoreLayout;

        [Category("XCI"), Description("加载配置之后事件")]
        public event EventHandler<EventArgs> AfterRestoreLayout;

        [Category("XCI"), Description("控件开始初始化事件")]
        public event EventHandler<EventArgs> BeforeInitialize;
        [Category("XCI"), Description("控件初始化结束事件")]
        public event EventHandler<EventArgs> AfterInitialize;


        protected virtual void OnBeforeInitialize()
        {
            BeforeInitialize?.Invoke(this, EventArgs.Empty);
        }

        protected virtual void OnAfterInitialize()
        {
            AfterInitialize?.Invoke(this, EventArgs.Empty);
        }

        protected override async void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;

            OnBeforeInitialize();

            //初始化表格配置
            InitSetting();

            //加载表格配置
            if (AutoLoadSetting)
            {
                await LoadSetting();
            }

            OnAfterInitialize();
        }

        public async Task LoadSetting()
        {
            if (SettingManager == null) SettingManager = new SettingStoreManager(this, Id);

            if (!string.IsNullOrEmpty(Id))
            {
                BeforeRestoreLayout?.Invoke(this, EventArgs.Empty);
                await SettingManager.LoadCurrentAsync();
                AfterRestoreLayout?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                Debug.WriteLine("请指定有效的ChartControlPlus-Id");
            }
        }

        public void InitSetting()
        {
            var allowCustomization = AllowCustomSetting && AppDefaultSettings.AllowCustomChartControlSetting;
            if (!allowCustomization) return;

            this.KeyDown += (s, e) =>
            {
                if (e.Control && e.Shift && e.Alt && e.KeyCode == Keys.F12)
                {
                    ShowSettingForm();
                    e.Handled = true;
                }
            };
        }

        /// <summary>
        /// 显示配置窗口
        /// </summary>
        public void ShowSettingForm()
        {
            var settingFrm = new FrmSettingChartForm();
            settingFrm.Init(this);
            settingFrm.Show();
        }

        /// <inheritdoc />
        public void SaveLayout(string xmlFile)
        {
            this.SaveToFile(xmlFile);
        }

        /// <inheritdoc />
        public void RestoreLayout(string xmlFile)
        {
            this.LoadFromFile(xmlFile);
        }
    }
}